package com.example.lib_common.http

import com.example.lib_common.logger.Logger
import com.google.gson.Gson
import okhttp3.Interceptor
import okhttp3.Response
import okio.Buffer
import org.json.JSONObject
import java.io.IOException
import java.nio.charset.Charset
import java.nio.charset.UnsupportedCharsetException


class OkHttpInterceptor : Interceptor {
    private val UTF8: Charset? = Charset.forName("UTF-8")

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val requestBody = request.body
        val startTime = System.nanoTime()
        var bodyStr = ""
        if (requestBody != null) {
            val buffer = Buffer()
            requestBody.writeTo(buffer)
            var charset = UTF8
            val contentType = requestBody.contentType()
            if (contentType != null) {
                charset = contentType.charset(UTF8)
            }
            bodyStr = buffer.readString(charset!!)
        }
        val header = Gson().toJson(request.headers)
        bodyStr = if (bodyStr === "") "无参数" else bodyStr

        val response = chain.proceed(request)
        val responseBody = response.body
        var bodyStrRespon = ""
        val source = responseBody!!.source()
        source.request(Long.Companion.MAX_VALUE)
        val buffer = source.buffer

        var charset = UTF8
        val contentType = responseBody.contentType()
        if (contentType != null) {
            try {
                charset = contentType.charset(UTF8)
            } catch (e: UnsupportedCharsetException) {
                e.printStackTrace()
            }
        }
        bodyStrRespon = buffer.clone().readString(charset!!)
        bodyStrRespon = if (bodyStrRespon === "") "无响应Body" else bodyStrRespon
        val endTime = System.nanoTime()
        val url =response.request.url.toString()
//        val msg = ("{\n\"Response\":\"code\":" + response.code
//                + "\t\"method\":\"" + request.method + "\"" + "\n"
//                + "\"请求url\":" + url + "\n"
//                + "\"startTime\":" + startTime + "\t\"endTime\":" + endTime + "\t\"responseTime\":" + (endTime - startTime) / 1e6 + "ms  \n"
//                + "\"请求头\":" + header + "\n"
//                + "\"请求参数\":" + bodyStr + "\n"
//                + "\"响应body\":" + bodyStrRespon + "\n"
//                + "}")
        val msg = buildString {
            append("{ ")
            append("\"Response\":\"code\":${response.code} | ")
            append("\"method\":\"${request.method}\" | ")
            append("\"请求url\":$url | ")
            append("\"startTime\":$startTime \"endTime\":$endTime \"responseTime\":${(endTime - startTime) / 1e6}ms | ")
            append("\"请求头\":$header | ")
            append("\"请求参数\":$bodyStr }")
            append("\n\"响应body\":$bodyStrRespon ")
        }
        if (response.code == 200)  //            LogUtil.Companion.gsonToInfo(msg);
            Logger.d(msg)
        else  //            LogUtil.Companion.error(msg);
            Logger.e(msg)
        return response
    }
}
